# _*_ coding: utf-8 _*_
"""
Time:     2024/7/25
Author:   lucybnada
File:     main.py
"""
# app.py
import json

from flask import Flask, render_template, request

from src.db.db_operate import DatabaseOperate

app = Flask(__name__, template_folder=r'../templates', static_folder='../static')


# @app.route('/')
# def index():
#     return render_template('index.html')
#
# @app.route('/api/data', methods=['GET'])
# def get_data():
#     # 示例数据，可以替换为实际的数据源
#     data = {
#         'message': 'Hello, World!',
#         'time': '2024-07-25'
#     }
#     return jsonify(data)

# @app.route('/')
# def index():
#     return render_template('index.html')

@app.route('/')
def index():
    client_ip = request.remote_addr
    return f'Your IP address is {client_ip}'


@app.route('/data', methods=['GET'])
def get_data():
    page = request.args.get('page', type=int, default=1)
    per_page = request.args.get('per_page', type=int, default=1000)
    op = DatabaseOperate
    database = 'dcv_web_c'
    name = 'web_szse_supervision_measure'
    total = op.get_total(database, name)
    comment = op.get_comment(database, name)
    comment['znz_create_time'] = '数据插入时间'
    comment['znz_update_time'] = '数据更新时间'
    rows = op.select_by_offset_limit(database, name, limit=per_page, offset=page)
    col_name = comment.keys()
    data = []
    item = [comment[key] for key in col_name]
    data.append(item)
    for row in rows:
        item = [row[key] for key in col_name]
        data.append(item)
    return render_template('index.html', data=json.dumps(data, default=str))


if __name__ == '__main__':
    app.run(debug=False)
